(function(){
function Pic(url,fn){
	var pic={img:new Image(),x:0,y:0,width:0,height:0};
	pic.img.src=url;
	pic.img.onload=function(){
		pic.width=this.width;
		pic.height=this.height;
		if(typeof fn=="function")
			fn(pic);
	}
	return pic;
}
var sPic=[];
for(var i=1;i<7;i++){
	sPic.push(Pic("images/s"+i+"_0.png"));
	sPic.push(Pic("images/s"+i+"_1.png"));
}
var mainPic=Pic("images/main_bg.png");
var callPic=Pic("images/btn_call.png")
var resetPic=Pic("images/btn_reset.png")
var lifePic=Pic("images/life.png");
var cpPic=Pic("images/cp.png")
var upPic=Pic("images/up.png")
var dialog_bg=Pic("images/dialog_bg.png");
var rollPic=[Pic("images/shake0.png"),Pic("images/shake1.png"),Pic("images/shake2.png")];
var sli;

window.View={
	startPage:$(".start"),
	endPage:$(".result"),
	recordList:$(".record ul"),
	cpWordBox:$(".main>div.cp"),
	upWordBox:$(".main>div.up"),
	rollBox:$("#rollCanvas"),
	tipBox:$(".main>div.tip-box"),
	rollSelectBox:$(".main>div.roll-select"),
	rollSelectNum:$(".main>div.roll-select span"),
	sound:$(new Audio()),
	mainCanvas:$("#mainCanvas"),
	playerCanvas:$("#playerCanvas")
}
View.sound.bankai(function(A){
	var list=[{url:"sound/鼓掌.mp3"},{url:"sound/喝倒彩.mp3"},{url:"sound/摇骰子.WAV",loop:true},{url:"sound/click_dice.mp3"}];
	var index=0;
	A.mount=function(el){
		A.list=list;
		A.index=index;
		el.src=list[index].url;
		el.loop=true;
	}
})
window.addEventListener("load",function(){
	View.playerCanvas.bankai(function(C){
		C.stage=function(c2d){
			this.frameRate=20;
			c2d.backgroundAlpha =0;
			C.drawImg(cpPic,0,17);
			C.drawImg(upPic,0,442);
		}
		C.showCpLife=function(n){
			C.drawImg(cpPic,0,17);
			for(var i=0;i<n;i++)
				C.drawImg(lifePic,20+i*20,130);
		}
		C.showUpLife=function(n){
			C.drawImg(upPic,0,442);
			for(var i=0;i<n;i++)
				C.drawImg(lifePic,20+i*20,555);
		}
	})
	
	View.mainCanvas.bankai(function(C){
		C.stage=function(c2d){
			c2d.backgroundAlpha =0;
			//C.drawImg(mainPic,0,0);
			this.frameRate=20;
		}
		C.mouseDown=function(c2d,x,y,e){
			if(x>430&&y>370&&x<510&&y<460){
				UI.showMyRoll(game.gameRole.diceArr);
			}
		}
	})
	
	View.rollBox.bankai(function(C){
		C.stage=function(c2d){
			c2d.backgroundAlpha =0;
			this.frameRate=3;
			this.loop=false;
		}
		C.draw=function(c2d){
			C.background();
			C.drawImg(rollPic[this.frameCount%3],0,0);
		}
		C.startRoll=function(){
			this.play().show();
		}
		C.stopRoll=function(){
			this.stop().hide();
		}
	})
	

	
	sli=View.rollSelectBox.find("li").click(function(){
		setTimeout("View.sound.p()",150);
		View.sound.play(3);
		
		var sn=this.getAttribute("sn")||0;
		var num=View.rollSelectNum.attr("num")||1;
		num++;
		if(num==11||sn!=View.rollSelectNum.attr("sn")){
			num=1;
		}
		View.rollSelectNum.attr("sn",sn).attr("num",num).css({"background-image":"url(images/n"+num+".png)","left":144+54*sn+"px"}).show();
		sli.each(function(i){
			this.style.backgroundImage="url(images/s"+(i+1)+"_"+(this.getAttribute("sn")==sn?1:0)+".png)";
		})
	})
})

window.UI={
	startGame:function(){
		View.startPage.hide();
		View.endPage.hide();
		UI.reSetSelectRoll();
		View.mainCanvas.drawImg(mainPic,0,0);
		View.playerCanvas.showCpLife(3);
		View.playerCanvas.showUpLife(3);
		game.init();
		game.play();
	},
	quitGame:function(){
		game.init();
		View.startPage.show();
	},
	showResult:function(r){
		View.endPage.css({"background-image":"url(images/big_"+r+".png)"}).show();
	},
	showCpLife:function(n){
		View.playerCanvas.showCpLife(n);
	},
	showUpLife:function(n){
		View.playerCanvas.showUpLife(n);
	},
	clearRound:function(){
		View.recordList.html("");
		UI.reSetSelectRoll();
	},
	showCpWords:function(name,w){
		var cwb=View.cpWordBox.html(w).show().delayFn(function(){cwb.hide()},3000);
		UI.record(name,w);
	},
	showUpWords:function(name,w){//玩家喊
		var uwb=View.upWordBox.html(w).show().delayFn(function(){uwb.hide()},3000);
		UI.record(name,w);
	},
	record:function(who,words){//显示历史记录
		View.recordList.append('<li><span class="usr-name">'+who+':</span><p>'+words+'</p></li>');
	},
	showMyRoll:function(nums){
		//View.rollSelectBox.hide();
		View.mainCanvas.drawImg(mainPic,0,0);
		View.mainCanvas.c2d.save();
		View.mainCanvas.c2d.scale()
		for(var i=0;i<nums.length;i++){
			View.mainCanvas.drawImg(sPic[nums[i]*2-1],210+i*40,390,32,32);
		}
		View.mainCanvas.c2d.restore();
	},
	showRoll:function(){//摇，新一轮开始场景
		View.rollBox.delayFn(function(){
			UI.hideRoll();
			UI.showMyRoll(game.gameRole.diceArr);
		},3000).startRoll();
		View.sound.play(2);
	},
	hideRoll:function(){//
		View.rollBox.stopRoll();
		View.sound.p();
	},
	shwoSelectRoll:function(){//显示选择盘
		View.rollSelectBox.show();
	},
	hideSelectRoll:function(){//显示选择盘
		View.rollSelectBox.hide();
	},
	bang:function(){
		UI.showTip("bang");
		setTimeout(function(){game.gameRole.open();},2000);
	},
	callSelectRoll:function(){
		var sn=View.rollSelectNum.attr("sn",sn);
		var num=View.rollSelectNum.attr("num",num);
		gameRoleHandFunc(num,sn);
	},
	reSetSelectRoll:function(){//重置
		View.rollSelectNum.attr("sn",0).attr("num",0).hide()
		sli.each(function(i){
			this.style.backgroundImage="url(images/s"+(i+1)+"_0.png)";
		})
	},
	showTip:function(name,soundIdx){
		//name -> [bang(开),cry(哭),happy(高兴),sad(伤心),]
		View.tipBox.find("img").attr("src","images/tip_"+name+".png");
		View.tipBox.show().delayFn(function(){
			View.tipBox.hide();
		},5000);
		if(!isNaN(soundIdx))
			View.sound.play(soundIdx).delayFn(function(){
				View.sound.p();
			},5000);
	}
}
})()